package com.hnucm.cooklife.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;
import com.hnucm.cooklife.R;
import com.hnucm.cooklife.base.utils.Constant;
import com.hnucm.cooklife.base.utils.LoginUtils;
import com.hnucm.cooklife.bean.base.Event;
import com.hnucm.cooklife.bean.db.Dish;
import com.hnucm.cooklife.ui.activity.LoginActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

public class RecomAdapter extends RecyclerView.Adapter<RecomAdapter.RecomHolder> {

    private Context mContext;
    private List<Dish> mDishList = new ArrayList<>();
    public static final int TRUE = 1;
    public static final int FALSE = 0;
//    private boolean isNightMode;

    //此变量类型为自定义接口类型
    private onItemClickListener mOnItemClickListener;

    //自定义接口实现点击事件，低耦合
    public interface onItemClickListener{
        void onSeleClickListener(View view,int position);
    }
    public void setOnItemClickListener(onItemClickListener listener){
        this.mOnItemClickListener = listener;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setDishList(List<Dish> dishList){
        mDishList.clear();
        mDishList.addAll(dishList);
        notifyDataSetChanged();
    }

    public RecomAdapter(Context context, List<Dish> dishList) {
        mContext = context;
        mDishList.addAll(dishList);
//        isNightMode = SPUtils.getInstance(Constant.CONFIG_SETTINGS)
//                .getBoolean(Constant.KEY_NIGHT_MODE,false);
    }

    @NonNull
    @Override
    public RecomHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.recom_item, parent, false);
        return new RecomHolder(view,mOnItemClickListener);
    }

    @Override
    public void onBindViewHolder(@NonNull RecomHolder holder, int position) {
        if(mDishList != null){
            Dish dish = mDishList.get(position);
            holder.mName.setText(
                    Html.fromHtml(String.valueOf(dish.dishname),Html.FROM_HTML_MODE_COMPACT)
            );
            holder.mAuthor.setText(
                    Html.fromHtml(String.valueOf(dish.username),Html.FROM_HTML_MODE_COMPACT)
            );

//            if(!LoginUtils.isLogin()){
//                holder.mCollect.setSelected(false);
//            } else {
//                holder.mCollect.setSelected(dish.collect == 1);
//            }

//            holder.mCollect.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if(!LoginUtils.isLogin()){
//                        Intent intent = new Intent(mContext, LoginActivity.class);
//                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                        mContext.startActivity(intent);
//                    } else {
//                        Event event = new Event();
//                        event.target = Event.TARGET_RECOM;
//                        event.type = dish.collect == 1 ? Event.TYPE_UNCOLLECT : Event.TYPE_COLLECT;
//                        event.data = dish.id +"";
//                        EventBus.getDefault().post(event);
//                    }
//                }
//            });
            Glide.with(mContext)
                    .load(dish.dishpic)
                    .fitCenter()
                    .centerCrop()
                    .into(holder.mImageView);

            Glide.with(mContext)
                    .load(dish.icon)
                    .fitCenter()
                    .centerCrop()
                    .into(holder.mHeader);

        }
    }

    @Override
    public int getItemCount() {
        return mDishList == null ? 0 : mDishList.size();
    }

    class RecomHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.dishName)
        TextView mName;

        @BindView(R.id.dishAuthor)
        TextView mAuthor;

        @BindView(R.id.dishPic)
        ImageView mImageView;

        @BindView(R.id.dishHeader)
        CircleImageView mHeader;

//        @BindView(R.id.collect_selector)
//        ImageView mCollect;

        public RecomHolder(@NonNull View itemView,final onItemClickListener onItemClickListener) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPosition = getAdapterPosition();
                    if(adapterPosition != RecyclerView.NO_POSITION){
                        onItemClickListener.onSeleClickListener(itemView,adapterPosition);
                    }
                }
            });
        }
    }
}
